<?php

namespace App\Http\Requests\Customers;

use App\Http\Requests\ApiRequest;
use App\Models\Customers\Customer;
use Illuminate\Validation\Rule;

/**
 * @property string $email
 * @property string $type
 * @property string $telephone
 * @property string $password
 * @property string $verify_code 手机验证码
 * @property string|null fullname
 * @property string dial_code
 */
class CustomerRegisterRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $type_rules = $this->getTypeRules($this->type);

        $rules = [
            'type'          => ['required', Rule::in(Customer::REGISTER_TYPES)],
            'password'      => 'required|min:6',
            'fullname'      => 'nullable|min:1|max:' . Customer::FULL_NAME_LENGTH,
            'oauth_type'    => 'nullable|string',
            'oauth_user_id' => 'nullable|int|min:1',
        ];

        return array_merge($rules, $type_rules);
    }

    private function getTypeRules($type)
    {
        switch ($type) {
            case Customer::REGISTER_TYPE_EMAIL:
                return $this->emailRules();
                break;
            case Customer::REGISTER_TYPE_TELEPHONE:
                return $this->telephoneRules();
                break;
            default :
                return [];
        }
    }


    private function emailRules()
    {
        return [
            'email' => 'required|email|unique:customer,email'
        ];
    }

    private function telephoneRules()
    {
        return [
            'telephone'   => 'required|string',
            'dial_code'   => 'nullable|int',
            'verify_code' => 'required',
        ];
    }
}
